package com.lovepick.fragment;

import java.lang.ref.WeakReference;
import java.util.List;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.ActionMode;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.lovepick.R;
import com.lovepick.adapter.OrderAdapter;
import com.lovepick.bean.Order;
import com.lovepick.http.HttpThread;
import com.lovepick.util.BaseUtils;
import com.lovepick.util.Constant;

public class RecmdFragment extends Fragment
{
	public static final String ARG_ROLE = "role";
	public static final String ARG_RECMD_JSON = "recmd_json";
	public static final String ARG_IS_FROM_RELEASE = "is_from_release";

	private static final String TAG = "RecmdFragment";

	private TextView mMineTitle;
	private TextView mOthersTitle;
	private ListView mOrderListView;
	private ProgressBar mOrderBar;

	private List<Order> mOrders = null;
	private OrderAdapter mAdapter = null;
	private HttpThread mThread = null;
	private RecmdHandler mHandler = null;
	private ActionMode mActionMode = null;
	private OnApplySuccessListener mCallBack;

	private String mRole = Constant.PASSENGER;

	/**
	 * @author Liutong
	 * <br>
	 * 订单申请成功后的回调
	 *
	 */
	public interface OnApplySuccessListener
	{
		public void onApplySuccess();
	}

	public void setApplySuccessListener(OnApplySuccessListener listener)
	{
		this.mCallBack = listener;
	}

	public static RecmdFragment createInstance(String recmdJson, boolean isFromRelease, String role)
	{
		RecmdFragment fragment = new RecmdFragment();
		Bundle args = new Bundle();
		args.putString(ARG_RECMD_JSON, recmdJson);
		args.putBoolean(ARG_IS_FROM_RELEASE, isFromRelease);
		args.putString(ARG_ROLE, role);
		fragment.setArguments(args);
		return fragment;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState)
	{
		super.onActivityCreated(savedInstanceState);
		Bundle args = getArguments();

	}

	private void findViews()
	{
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState)
	{
		return inflater.inflate(R.layout.order_recmd, container, false);
	}

	private void applyOrder(Order order)
	{
		Log.i(TAG, "---applyOrder---");
	}

	private static class RecmdHandler extends Handler
	{
		private WeakReference<RecmdFragment> mReference;
		private RecmdFragment mFragment;

		public RecmdHandler(RecmdFragment fragment)
		{
			mReference = new WeakReference<RecmdFragment>(fragment);
		}

		@Override
		public void handleMessage(Message msg)
		{
			mFragment = mReference.get();
			switch (msg.what)
			{
				case Constant.MSG_HTTP_SUCCESS:
					mFragment.mOrderBar.setVisibility(View.GONE);
					String result = msg.getData().getString(Constant.HTTP_RESULT);
					if (Constant.HTTP_ORDER_APPLY_SUCCESS.equals(result))
					{
						BaseUtils.showToast(mFragment.getActivity(), R.string.apply_success);
						mFragment.mCallBack.onApplySuccess();
					}
					else
					{
						BaseUtils.showToast(mFragment.getActivity(), R.string.apply_fail);
					}
					break;

				case Constant.MSG_HTTP_ERROR:
					mFragment.mOrderBar.setVisibility(View.GONE);
					BaseUtils.showToast(mFragment.getActivity(), R.string.network_connnect_fail);
					break;

				default:
					break;
			}
		}
	}
}
